import { Component } from "@angular/core";
import { NavController, NavParams, AlertController } from "ionic-angular";
import { HttpService } from "./../../../providers/http.service";
import { CommonFunc } from "./../../../providers/commonFunc";
import { appConfig } from "../../../providers/config";
import {FileTransfer, FileTransferObject} from "@ionic-native/file-transfer";
import {FileChooser} from "@ionic-native/file-chooser";
import {FileOpener} from "@ionic-native/file-opener";
import {File} from "@ionic-native/file";

@Component({
  templateUrl: "notice-detail.html"
})

//通知详情
export class NoticeDetail {
  private noticeBean: NoticeDetailBean = new NoticeDetailBean();
  private appConfig = appConfig.IP_URL + appConfig.API.getUpload;
  private tempAppConfig = appConfig.IP_URL + appConfig.API.getUpload;
  private imageList: any = [];
  constructor(
    private navController: NavController,
    private navParams: NavParams,
    private httpService: HttpService,
    private commonFunc: CommonFunc,
    private alertCtrl: AlertController,
    private fileChooser: FileChooser,
    private transfer: FileTransfer,
    private fileOpener: FileOpener,
    private file: File,
  ) {
    let id = this.navParams.get("id");
    this.httpService.noticeFindById(id).subscribe(data => {
      if (data.status != 200) {
        let toast = this.commonFunc.showInsideErrorToast(data);
        toast.present();
        return;
      }
      this.noticeBean = data.data;
      if (this.noticeBean.fileUrls) {
        this.imageList = this.noticeBean.fileUrls.split(",");
      }
    });
  }

  downloadDoc(){
    if(this.noticeBean.fileUrls != null && this.noticeBean.fileUrls != ""){
      this.download(this.noticeBean.fileUrls.split(",")[0]);
    }
  }

  download(fileName) {
    const url = this.tempAppConfig + fileName;
    if (true) {
      let alert = this.alertCtrl.create({
        title: '下载进度：0%',
        enableBackdropDismiss: false,
        buttons: ['后台下载']
      });
      alert.present();
      console.log("文件下载路径："+url);
      const fileTransfer: FileTransferObject = this.transfer.create();
      this.file.createDir(this.file.externalRootDirectory, "appFolder", true).then(link => {
        const localPath = this.file.externalRootDirectory + fileName;
        console.log("本地存放路径："+localPath);
        fileTransfer.download(url, localPath).then((entry) => {
          const nativePath = entry.toURL();
          this.fileOpener.open(nativePath, this.httpService.getFileMimeType(this.httpService.getFileType(fileName))).then(
              () => {
              });
        });
      })

      fileTransfer.onProgress((event: ProgressEvent) => {
        let num = Math.floor(event.loaded / event.total * 100);
        if (num === 100) {
          alert.dismiss();
        } else {
          let title = document.getElementsByClassName('alert-title')[0];
          title && (title.innerHTML = '下载进度：' + num + '%');
        }
      });
    }
  }

  doConfirm() {
    let param = {
          id: this.noticeBean.id,
          isRead: "1"
      };
      this.httpService.updateReadState(param).subscribe(data => {
          if (data.status == 200) {
              const alert1 = this.alertCtrl.create({
                  // title: '',
                  subTitle: "更新成功,已读的消息请到历史中查看",
                  buttons: ["确定"]
              });
              alert1.present();
          } else {
              const alert1 = this.alertCtrl.create({
                  title: "失败",
                  subTitle: "更新消息状态失败，请重试",
                  buttons: ["确定"]
              });
              alert1.present();
          }
      });
  }
}

class NoticeDetailBean {
  id: string;
  title: string;
  content: string;
  fileUrls: string;
  sendUserId: string;
  receiveUserId: string;
  isRead: string;
  noticeTime: string;
  receivePartyId: string;
  elevatoriType: string;
  elevatoriMonId: string;
  repairInfoId : string;
}
